package com.test19_多线程;

/**
 * Runnable接口
 */
public class Demo02_Runnable {

    public static void main(String[] args) {
//        test1();
        test2();
    }

    /**
     * 通过Thread启动Runnable定义的线程类对象
     */
    public static void test1() {
        MyThread2 threadA = new MyThread2("Yootk-A线程");
        MyThread2 threadB = new MyThread2("Yootk-B线程");
        MyThread2 threadC = new MyThread2("Yootk-C线程");
        new Thread(threadA).start();    // 启动线程并调用run()方法
        new Thread(threadB).start();
        new Thread(threadC).start();
    }

    /**
     * 使用Lambda进行多线程的创建
     */
    public static void test2() {
        for (int x = 0; x < 3; x++) {
            String name = "Yootk-" + (char)('A'+x) + "线程对象";
            new Thread(() -> {
                for (int y = 0; y < 10; y++) {
                    System.out.println("【" + name + "】线程执行，当前的循环次数为：num = " + y);
                }
            }).start();
        }
    }
}

class MyThread2 implements Runnable {
    private String name;

    public MyThread2() {
    }

    public MyThread2(String name) {
        this.name = name;
    }

    /** 线程运行的主方法 */
    @Override
    public void run() {
        for (int x = 0; x < 10; x++) {
            System.out.println("【" + this.name + "】线程执行，当前的循环次数为：x = " + x);
        }
    }
}
